package puyu.manage.service.file.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.web.multipart.MultipartFile;
import puyu.manage.service.file.entity.SysTFileUpload;
import puyu.manage.service.file.dao.SysTFileUploadMapper;
import org.springframework.stereotype.Service;
import puyu.manage.service.file.entity.vo.FileUploadVo;
import puyu.manage.service.minio.MinioUtil;
import puyu.manage.service.utils.IpUtils;

import javax.annotation.Resource;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 文件表(SysTFileUpload)表服务实现类
 *
 * @author makejava
 * @since 2024-06-07 15:12:51
 */
@Service
public class SysTFileUploadServiceImpl extends ServiceImpl<SysTFileUploadMapper, SysTFileUpload> implements SysTFileUploadService {

    @Resource
    private SysTFileUploadMapper sysTFileUploadMapper;

    @Resource
    private MinioUtil minioUtil;

    /**
     * 单个上传
     *
     * @param file
     */
    @Override
    public SysTFileUpload upload(MultipartFile file) {

        SysTFileUpload fileUpload = new SysTFileUpload();
        //文件名 格式xxx.xxx
        String fileName = minioUtil.putObject(file);
        fileUpload.setFileName(fileName);

        String ip = IpUtils.getByIp();
        //url
//        String url = "http://" + ip + ":9000/puyu/" + fileName;//本机地址
        String url = "http://47.101.188.211:9000/puyu/" + fileName;//生产地址
        fileUpload.setFileUrl(url);
        //文件大小
        long size = file.getSize();
        fileUpload.setFileSize(size);

        //文件后缀
        String suffix = file.getOriginalFilename();
        int dotIndex = suffix.lastIndexOf('.');
        String substring = suffix.substring(dotIndex + 1);
        fileUpload.setFileSuffix(substring);
        fileUpload.setCreateTime(new Date());
        fileUpload.setStatus("0");
        fileUpload.setIsDel("0");

        this.save(fileUpload);

        return fileUpload;
    }

    /**
     * 批量上传
     **/
    @Override
    public void uploads(MultipartFile[] file) {

        List<SysTFileUpload> sysTFileUploads = new ArrayList<>();

        for (MultipartFile multipartFile : file) {

            SysTFileUpload fileUpload = new SysTFileUpload();
            //文件名 格式xxx.xxx
            String fileName = minioUtil.putObject(multipartFile);
            fileUpload.setFileName(fileName);

            String ip = IpUtils.getByIp();
            //url
            String url = "http://" + ip + ":9000/puyu/" + fileName;
            fileUpload.setFileUrl(url);
            //文件大小
            long size = multipartFile.getSize();
            fileUpload.setFileSize(size);

            //文件后缀
            String suffix = multipartFile.getOriginalFilename();
            int dotIndex = suffix.lastIndexOf('.');
            String substring = suffix.substring(dotIndex + 1);
            fileUpload.setFileSuffix(substring);
            fileUpload.setCreateTime(new Date());
            fileUpload.setStatus("0");
            fileUpload.setIsDel("0");
            sysTFileUploads.add(fileUpload);
        }

        this.saveBatch(sysTFileUploads);

    }



}

